package com.dream.wukong.common.core.importexport.csv.parser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;

/**
 * @Author: wuyichao
 * @Description:
 * @Date: Created in 16:35 2018/9/17.
 * @Modified:
 */
public abstract class AbstractParser {

    private static final Logger logger = LoggerFactory.getLogger(AbstractParser.class);


    private final String name;
    private int rowCount;
    private int blackhole; // something to keep values from processed objects to
    // avoid unwanted JIT's dead code removal

    protected String encoding = "gbk";

    protected AbstractParser(String name) {
        this.name = name;
    }

    public final String getName() {
        return name;
    }

    protected boolean process(Object row) {
        if (row == null) {
            return false;
        }

        blackhole += System.identityHashCode(row);
        rowCount++;
        return true;
    }

    public void resetRowCount() {
        rowCount = 0;
    }

    public int getRowCount() {
        return rowCount;
    }

    public String getBlackhole() {
        return String.valueOf(blackhole);
    }

    protected Reader toReader(File input) {
        try {

            return new FileReader(input);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }

    protected Reader toReader(File input, String encoding) {
        try {

            DataInputStream in=new DataInputStream(new FileInputStream(input));
            BufferedReader br=new BufferedReader(new InputStreamReader(in,encoding));
            return br;
//			return new InputStreamReader(new FileInputStream(input),encoding);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e);
        }
    }

    protected Writer toWriter(File out) {
        try {
            return new FileWriter(out);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    protected Writer toWriter(File out, String encoding) {
        try {
            return new OutputStreamWriter(new FileOutputStream(out,true),encoding);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e);
        }
    }

    public String getEncoding() {
        return encoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public abstract void processRows(File input);

    /**
     * 读取CSV文件内容到list数组
     * @param input
     * @return
     */
    public abstract List<String[]> parseRows(File input);

    /**
     * 将数据写入临时文件
     * @return
     */
    public abstract File writeRows(List<Object[]> rows);

    /**
     * 将数据写入指定文件
     * @param filepath
     * @return
     */
    public abstract File writeRows(List<Object[]> rows,String filepath) ;

}